package com.truyen.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.googlecode.ehcache.annotations.Cacheable;
import com.truyen.persistence.dao.TruyenTrinhThamDao;
import com.truyen.persistence.entity.TacGia;
import com.truyen.persistence.entity.ChapterTam;
import com.truyen.persistence.entity.ChapterTruyenTrinhTham;
import com.truyen.persistence.entity.ChapterTmpTruyenTrinhTham;
import com.truyen.persistence.entity.TruyenTrinhTham;
import com.truyen.service.ChapterTamService;
import com.truyen.service.ChapterTruyenTrinhThamService;
import com.truyen.service.ChapterTmpTruyenTrinhThamService;
import com.truyen.service.TruyenTrinhThamService;
import com.truyen.utils.PagingInfo;


@Service
public class TruyenTrinhThamServiceImpl implements TruyenTrinhThamService {
	
	@Autowired
	private TruyenTrinhThamDao truyenTrinhThamDao;
	
	@Autowired
	private ChapterTruyenTrinhThamService chapterTruyenTrinhThamService;
	
	@Autowired
	private ChapterTmpTruyenTrinhThamService chapterTmpTruyenTrinhThamService;
	
	@Autowired
	private ChapterTamService chapterTamService;

	@Override
	public void save(TruyenTrinhTham entity) {
		truyenTrinhThamDao.save(entity);
	}

	@Override
	public TruyenTrinhTham findById(Long id) {
		return truyenTrinhThamDao.findById(id);
	}

	@Override
	public List<TruyenTrinhTham> getAll() {
		return truyenTrinhThamDao.findAll();
	}

	@Override
	public TruyenTrinhTham findByProperty(String column, Object value) {
		List<TruyenTrinhTham> lst = truyenTrinhThamDao.findByProperty(column, value);
		return (lst.size() > 0)?lst.get(0):null;
	}

	@Override
	public List<TruyenTrinhTham> getLstComicLinkImageNullBlogTruyen(int sourceBy){
		return truyenTrinhThamDao.getLstComicLinkImageNullBlogTruyen(sourceBy);
	}

	@Override
	@Cacheable(cacheName="truyenTrinhThamCacheDataController")
	public List<TruyenTrinhTham> getAllLst(String kwSearch, String sort, String sortBy,	PagingInfo pInfo) {
		return truyenTrinhThamDao.getAllLst(kwSearch,sort,sortBy,pInfo);
	}

	@Override
	public void setHot(Long id) {
		try{
			TruyenTrinhTham entity = findById(id);
			boolean isHot = entity.getIsHot();
			if(isHot){
				entity.setIsHot(false);
			}else{
				entity.setIsHot(true);
			}
			truyenTrinhThamDao.merge(entity);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public void setIsDelete(Long id) {
		try{
			TruyenTrinhTham entity = findById(id);
			boolean isDelete = entity.getIsDelete();
			if(isDelete){
				entity.setIsDelete(false);
			}else{
				entity.setIsDelete(true);
			}
			truyenTrinhThamDao.merge(entity);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	@Override
	@Cacheable(cacheName="lstTruyenTrinhThamCacheTruyenController")
	public List<TruyenTrinhTham> getListTruyenBy(String orderByColumn,int begin, int max) {
		return truyenTrinhThamDao.getListTruyenBy(orderByColumn,begin,max);
	}

	@Override
	public List<TruyenTrinhTham> getListTruyenRandom(int maxLimit) {
		return truyenTrinhThamDao.getListTruyenRandomBy(maxLimit);
	}

	@Override
	public void updateTotalChapterr(Long id, float totalChapter) {
		try{
			TruyenTrinhTham entity = findById(id);
			entity.setTotalChapter(totalChapter);
			truyenTrinhThamDao.merge(entity);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public List<TruyenTrinhTham> getLstTruyenBySourceBy(int sourceBy) {
		return truyenTrinhThamDao.getLstTruyenBySourceBy(sourceBy);
	}

	@Override
	public void updateViews(Long id, long views, long viewsWeek, long viewsMonth) {
		try{
			TruyenTrinhTham entity = findById(id);
			entity.setViews(views);
			entity.setViewsWeek(viewsWeek);
			entity.setViewsMonth(viewsMonth);
			truyenTrinhThamDao.merge(entity);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	@Cacheable(cacheName="lstTruyenTrinhThamTimKiemCacheTruyenController")
	public List<TruyenTrinhTham> getLstByProperty(String column, Object value, int begin, int maxLimit) {
		return truyenTrinhThamDao.getLstByProperty(column,value, begin, maxLimit);
	}

	@Override
	public List<Object> getObjectByNameUnsigned(String kwSearch, int max, String typeSearch) {
		return truyenTrinhThamDao.getObjectByNameUnsigned(kwSearch,max,typeSearch);
	}

	@Override
	public List<TruyenTrinhTham> getLstByPropertyWithPaging(String column, String value, String sortBy, PagingInfo pInfo) {
		return truyenTrinhThamDao.getLstByPropertyWithPaging(column, value, sortBy, pInfo);
	}

	@Override
	public void updateLiked(Long idTruyen, long liked) {
		try{
			TruyenTrinhTham entity = findById(idTruyen);
			entity.setLiked(liked);
			truyenTrinhThamDao.merge(entity);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	@Transactional
	public void delete(Long id) {
		try{
			TruyenTrinhTham entity = findById(id);
			List<ChapterTam> lstChapterTam = chapterTamService.getByTruyen(entity);
			if(lstChapterTam != null){
				int sizeList = lstChapterTam.size();
				for(int i=0;i<sizeList;i++){
					ChapterTam chapterTam = lstChapterTam.get(i);
					chapterTamService.delete(chapterTam.getId());
				}
			}
			List<ChapterTmpTruyenTrinhTham> lstchapterTmpTruyenTrinhTham = chapterTmpTruyenTrinhThamService.getByTruyen(entity);
			if(lstchapterTmpTruyenTrinhTham != null){
				int sizeList = lstchapterTmpTruyenTrinhTham.size();
				for(int i=0;i<sizeList;i++){
					ChapterTmpTruyenTrinhTham chapterTmpTruyenTrinhTham = lstchapterTmpTruyenTrinhTham.get(i);
					ChapterTruyenTrinhTham chapterTruyenTrinhTham = chapterTmpTruyenTrinhTham.getChapterTruyenTrinhTham();
					chapterTruyenTrinhThamService.delete(chapterTruyenTrinhTham.getId());
					chapterTmpTruyenTrinhThamService.delete(chapterTmpTruyenTrinhTham.getId());
				}
			}
			truyenTrinhThamDao.delete(entity);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	@Transactional
	public void updateColumnString(long id, String column, String value) {
		try {
			TruyenTrinhTham entity = findById(id);
			if(column.equals("linkImage")){
				entity.setLinkImage(value);
			}
			truyenTrinhThamDao.merge(entity);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}

	@Override
	@Cacheable(cacheName="lstTruyenTrinhThamOrderByCacheTruyenController")
	public List<TruyenTrinhTham> getLstByPropertyWithOrderByPaging(String orderBy, PagingInfo pInfo) {
		return truyenTrinhThamDao.getLstByPropertyWithOrderByPaging(orderBy,pInfo);
	}

	@Override
	public List<TruyenTrinhTham> getLstByTacGiaWithPaging(TacGia tacGia, String orderBy, PagingInfo pInfo) {
		return truyenTrinhThamDao.getLstByTacGiaWithPaging(tacGia,orderBy,pInfo);
	}

	@Override
	public List<TruyenTrinhTham> getLstByTacGiaLimit(TacGia tacGia, int begin, int maxLimit) {
		return truyenTrinhThamDao.getLstByTacGiaLimit(tacGia, begin, maxLimit);
	}

	@Override
	public void update(Long idTruyen, String name, String alias,
			String linkImage, String nameImage, String info, TacGia tacGia,
			String categories, float totalChapter, boolean isDelete,
			boolean isFinished, boolean isHot, long liked, long views,
			long viewsWeek, long viewsMonth, int sourceBy,
			String linkSource, String translator) {
		try {
				TruyenTrinhTham entity = findById(idTruyen);
				if(entity != null){
					entity.setName(name);
					entity.setAlias(alias);
					entity.setLinkImage(linkImage);
					entity.setNameImage(nameImage);
					entity.setInfo(info);
					entity.setTacGia(tacGia);
					entity.setCategories(categories);
					entity.setTotalChapter(totalChapter);
					entity.setIsDelete(isDelete);
					entity.setIsFinished(isFinished);
					entity.setIsHot(isHot);
					entity.setLiked(liked);
					entity.setViews(views);
					entity.setViewsWeek(viewsWeek);
					entity.setViewsMonth(viewsMonth);
					entity.setSourceBy(sourceBy);
					entity.setLinkSource(linkSource);
					entity.setTranslator(translator);
					truyenTrinhThamDao.merge(entity);
				}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}

	@Async
	@Override
	public void updateViewsWeek() {
		List<TruyenTrinhTham> lstTruyen = getAll();
		if(lstTruyen.size() > 0){
			int size = lstTruyen.size();
			for(int i=0; i < size; i++){
				TruyenTrinhTham truyen = lstTruyen.get(i);
				truyen.setViewsWeek(0);
				truyenTrinhThamDao.merge(truyen);
			}
		}
	}

	@Async
	@Override
	public void updateViewsMonth() {
		List<TruyenTrinhTham> lstTruyen = getAll();
		if(lstTruyen.size() > 0){
			int size = lstTruyen.size();
			for(int i=0; i < size; i++){
				TruyenTrinhTham truyen = lstTruyen.get(i);
				truyen.setViewsMonth(0);
				truyenTrinhThamDao.merge(truyen);
			}
		}
	}

	@Override
	public List<TruyenTrinhTham> findByPropertyNotNull(String property) {
		return truyenTrinhThamDao.findByPropertyNotNull(property);
	}

}
